import config from "../../utils/config.js";
// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfoflag: false,
    sessionkey:'',
    user:{},//用户信息
    refresh: false,//是否刷新中
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.code){
      wx.setStorageSync('code', options.code)
    }
    if(options.scene){
      let code = decodeURIComponent(options.scene)
      let arrinfo=code.split('=');
      if (arrinfo[0] =='parent_id'){
        wx.setStorageSync('code', arrinfo[1])
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that=this;
    if(!wx.getStorageSync('token')){
      that.setData({
        userinfoflag:true
      })
    }else{
      that.getMemberInfo();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const that=this;
    return {
      title: that.data.user.nickname+'邀请您加入卡丁车体验平台',
      path: '/pages/index/index?code=' + that.data.user.parent_id
    }
  },

  bindGetUserInfo:function(e){
    const that=this;
    wx.showLoading({
      title:'加载中...'
    })
    wx.login({
      success: function (res) {
        config.post('getSessionKey', { code: res.code }, (res) => {
          that.setData({
            sessionkey: res.data.session_key
          })
          wx.getUserInfo({
            lang:'zh_CN',
            success:function(result){      
              config.post('decryptMinData', {
                data: result.encryptedData,
                iv: result.iv,
                session_key: that.data.sessionkey,
                parent_id: wx.getStorageSync('code') ? wx.getStorageSync('code') : ''
              }, (res) => {
                that.setData({
                  userinfoflag: false
                })
                wx.hideLoading()
                wx.removeStorageSync('code');
                wx.setStorageSync('token', res.data.token)
                that.getMemberInfo();
              }) 
            },
            fail:function(){
              wx.showToast({
                title: '授权失败',
                icon:'none'
              });
              wx.hideLoading();
            }
          })
        })
      }
    })
    
  },

  //获取用户信息
  getMemberInfo:function(){
    const that=this;
    config.post('getMemberInfo', {token:wx.getStorageSync('token')},(res)=>{
        that.setData({
          user:res.data
          })
    })
  },

  //刷新用户信息
  refreshMemberInfo: function () {
    const that = this;
    if (!that.data.refresh){
      that.setData({
        refresh: true
      })
      config.post('getMemberInfo', { token: wx.getStorageSync('token') }, (res) => {
        that.setData({
          user: res.data,
        })
        setTimeout(() => {
          that.setData({
            refresh: false
          })
        }, 500)
      },()=>{
        setTimeout(() => {
          that.setData({
            refresh: false
          })
        }, 500)
      })
    }
  },

  //扫码用车
  scan:function(){
    const that=this;
    wx.scanCode({
      success(res) {
        console.log(res);
        let result = decodeURIComponent(res.path)
        let arrinfo = result.split('=');
        console.log(arrinfo)
        if(that.data.user)
          config.post('getStore', { store_id: arrinfo[2]},(ress)=>{
            const money = ress.data.price;
            wx.showModal({
              title: '确认支付？',
              content: that.data.user.coupon > 0 ? '此次消费将使用一张体验卷' : '此次消费将支付￥' + money,
              success(res) {
                if (res.confirm) {
                  config.post('scanningCode', {
                    token: that.data.user.token,
                    store_id: arrinfo[2],
                    money: money,
                  }, (res) => {
                    that.getMemberInfo();
                    wx.showToast({
                      title: '支付成功',
                    })
                  })
                } else if (res.cancel) {
                  //用户点击取消
                }
              }
            })
          })
      },
      fail:function(){
        //扫码取消
      }
    })
  },

  previewImage:function(){
    const that=this;
    wx.previewImage({
      current: that.data.user.code, // 当前显示图片的http链接
      urls: [that.data.user.code] // 需要预览的图片http链接列表
    })
  },

  //前往下级页面
  tochild:function(){
   wx.navigateTo({
     url: '/pages/mychild/index',
   })
  }
})